package com.blankland.dataanalysis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blankland.dataanalysis.domain.Role;
import com.blankland.dataanalysis.result.Result;
import com.blankland.dataanalysis.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 角色管理
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation("查询角色：所有")
    @GetMapping("/findAll")
    public Result findAll(){
        List<Role> list = roleService.list();
        return Result.ok(list);
    }

    @ApiOperation("查询角色：根据id")
    @GetMapping("/findRoleById/{id}")
    public Result findRoleById(@PathVariable Long id){
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 添加角色：
     * @RequestBody注解的作用：
     *      1、限制提交方式不能是get
     *      2、接收到的json数据转成实体对象
     * @param role
     * @return
     */
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result saveRole(@RequestBody Role role){
        boolean flag = roleService.save(role);
        if (true == flag){
            return Result.ok();
        }else {
            return Result.fail();

        }
    }

    @ApiOperation("修改角色：根据id")
    @PutMapping("/update")
    public Result updateRoleById(@RequestBody Role role){
        boolean flag = roleService.updateById(role);
        if (true == flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("删除角色（逻辑删除）：根据id")
    @DeleteMapping("/remove/{id}")
    public Result removeRole(@PathVariable Long id){
        boolean flag = roleService.removeById(id);
        if (true == flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("批量删除（逻辑删除）")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean flag = roleService.removeBatchByIds(idList);
        if (true == flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    @ApiOperation("分页查询")
    @GetMapping("/findRoleByPage")
    public Result findRoleByPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize){
        //创建分页对象
        Page<Role> rolePage = new Page<>(pageNum, pageSize);
        //分页查询
        Page<Role> page = roleService.page(rolePage);
        return Result.ok(page);
    }

    @ApiOperation("分页+模糊查询")
    @GetMapping("/findRoleByPageAndCondition")
    public Result findRoleByPageAndCondition(@RequestParam Integer pageNum,
                                             @RequestParam Integer pageSize,
                                             @RequestParam(defaultValue = "") String roleName){

        // 分页对象
        Page<Role> rolePage = new Page<>(pageNum, pageSize);
        //分页条件查询
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.like("role_name",roleName);//模糊查询
        wrapper.orderByDesc("id");//倒序
        Page<Role> page = roleService.page(rolePage, wrapper);
        return Result.ok(page);
    }

}
